package com.atguigu.structures.stack;

/**
 * 计算器辅助工具
 *
 * @author xr-8
 * @date 2022-07-20
 * @time 23:09
 */
public class CalculatorUtil {

    /**
     * 返回对应运算符的优先级数字
     * @param operation
     * @return
     */
    public static int getValue(String operation) {
        int result = 0;
        switch (operation) {
            case "+":
                result = 1;
                break;
            case "-":
                result = 1;
                break;
            case "*":
                result = 2;
                break;
            case "/":
                result = 2;
                break;
            default:
                System.out.println("不存在该运算符" + operation);
                break;
        }
        return result;
    }

    /**
     * 返回运算符的优先级.
     * 数字越大，则优先级就越高.
     * @param op
     * @return
     */
    public static int priority(int op) {
        if(op == '*' || op == '/'){
            return 1;
        } else if (op == '+' || op == '-') {
            return 0;
        } else {
            return -1; // 假定目前的表达式只有 +, - , * , /
        }
    }

    /**
     * 判断是不是一个运算符
     * @param val
     * @return
     */
    public static boolean isOp(char val) {
        return val == '+' || val == '-' || val == '*' || val == '/';
    }

    /**
     * 计算
     * @param num1
     * @param num2
     * @param oper
     * @return
     */
    public static int calc(int num1, int num2, int oper) {
        int res = 0; // res 用于存放计算的结果
        switch (oper) {
            case '+':
                res = num1 + num2;
                break;
            case '-':
                res = num2 - num1;// 注意顺序
                break;
            case '*':
                res = num1 * num2;
                break;
            case '/':
                res = num2 / num1;
                break;
            default:
                break;
        }
        return res;
    }

}
